'use strict';

import {ajax, nameSpace } from 'utils/index';
import jsonp from 'jsonp';

let ns = nameSpace('ZhekouLimitEditItems');

export const GET_LIST_DONE = ns('GET_LIST_DONE');
export const CLEAR_LIST = ns('CLEAR_LIST');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const RESET_ITEM = ns('RESET_ITEM');
export const EDIT_ITEM = ns('EDIT_ITEM');
export const REMOVE_ITEM = ns('REMOVE_ITEM');
export const LOAD_START = ns('LOAD_START');
export const LOAD_ERR = ns('LOAD_ERR');

export function clearList() {
  return {type: CLEAR_LIST};
}
export function closeDialog() {
  return {type: CLOSE_DIALOG,visible:false};
}
export function removeItem(activity_id,detail_id) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		if(window.workbench){
			//千牛环境执行
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.common.item.detail.delete',
					param : {activity_id,detail_id},
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			//web环境执行创建折扣
			ajax({
				  api: 'removeLimitInActive',
				  method: 'GET',
				  data:{activity_id,detail_id}
				},(json) => {
					dispatch({
					  type: REMOVE_ITEM,
					  activity_id,
					  detail_id
					});
				},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
					});
			});
		}
	}
}
export function resetItem(ztype,item,value) {
	if(ztype=="finalPrice"){
		ztype = "decreaseAmount";
		value = (item.price - value);
	}
	let finaPrice = item.price;
	switch(ztype){
		case "decreaseAmount":
			item.promotion_type=0;
			finaPrice = item.price - value;
		break;
		case "discountRate" :
			item.promotion_type=1;
			finaPrice = item.price * (value/10);
		break;
	}
	item.finaPrice = finaPrice;
	item.value = value;
	item.ztype = ztype;
	item.promotion_value = value;
  return {type: RESET_ITEM,item};
}
export function setLimit(item,limitValue) {
	item.is_limit_buy = 1;
	item.limit_count = limitValue;
	return {type: RESET_ITEM,item};
}
export function getList(params) {
    return (dispatch) => {
    //发送请求前
    dispatch({
      type: LOAD_START,
      data: params
    });
	if(window.workbench){
		//千牛环境执行
		  window.QN.top.invoke( {
				cmd : 'taobao.promotionmisc.common.item.detail.list.get',
				param : params,
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
					//先读取列表，再按照ids读取商品信息
					 window.QN.top.invoke( {
						cmd : 'taobao.items.seller.list.get',
						param : {ids:params.ids},
						method:"post",
						success : function(rsp_1, cmd_1, param_1) {
							console.log(rsp_1,param_1);
						},
						error : function(msg, cmd, param) {
							console.log(msg,param);
						}
					} );
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		ajax({
			  api: 'getItemsInLimitActive',
			  method: 'GET',
			  data:params
			},(json) => {
			//成功后
			    if(json.total_count==0){
					let item_json = {items:{item:[]},total_count:0};
					dispatch({
					  type: GET_LIST_DONE,
					  data: item_json
					});
					return;
				}
				if(json.total_count>0 && !(json.detail_list.common_item_detail instanceof Array)){
					json.detail_list.common_item_detail = Array(json.detail_list.common_item_detail);
				}
				let ids = [];
				json.detail_list.common_item_detail.map(det=>{
					ids.push(det.item_id);
				});
				ajax({
				  api: 'getItemsByIds',
				  method: 'POST',
				  data:{ids:ids.join(",")}
				},(item_json) => {
					if(item_json.items.item && !(item_json.items.item instanceof Array)){
						item_json.items.item = Array(item_json.items.item);
					}
					item_json.items.item.map((item,key)=>{
						json.detail_list.common_item_detail.map((det)=>{
							if(det.item_id==item.num_iid){
								item_json.items.item[key].activity_id = det.activity_id;
								item_json.items.item[key].detail_id = det.detail_id;
								item_json.items.item[key].promotion_type = det.promotion_type;
								item_json.items.item[key].promotion_value = det.promotion_value/100;
								item_json.items.item[key].ztype = det.promotion_type==0?"decreaseAmount":"discountRate";
								item_json.items.item[key].value = det.promotion_value/100;
								item_json.items.item[key].is_limit_buy = 1;
								item_json.items.item[key].limit_count = det.limit_count;
								switch(det.promotion_type){
									case 0 :
										item_json.items.item[key].finaPrice = (parseInt(item.price) - det.promotion_value/100);
									break;
									case 1 :
										item_json.items.item[key].finaPrice = (parseInt(item.price) * (det.promotion_value/1000));
									break;
								}
							};
						});
					});
					item_json.total_count=json.total_count;
					dispatch({
					  type: GET_LIST_DONE,
					  data: item_json
					});
				},(json) =>{
					const login = (json.code==210);
					dispatch({
						type: LOAD_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:login
					});
				});
				

			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  };
}

export function editItemInActive(itemset) {

	let params = {
		activity_id : itemset.activity_id,
		item_id:itemset.num_iid,
		price:itemset.price,
		promotion_value:itemset.promotion_value,
		promotion_type:itemset.promotion_type,
		detail_id:itemset.detail_id,
		is_limit_buy:itemset.is_limit_buy,
		limit_count:itemset.limit_count
	}
    return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START,
		  data: params
		});
		ajax({
			  api: 'editLimitItemInActive',
			  method: 'GET',
			  data:params
			},(json) => {
				dispatch({
					type: EDIT_ITEM,
					itemset
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:"")+" 请注意，已结束的活动不能修改",
					loginFalse:login
				});
			}
		);
	}
}